// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Copyright (C) 2008 Vignette Corporation  All rights reserved.
// THIS PROGRAM IS CONFIDENTIAL AND AN UNPUBLISHED WORK AND TRADE SECRET
// OF THE COPYRIGHT HOLDER, AND DISTRIBUTED ONLY UNDER RESTRICTION.
// EXCEPT AS EXPLICITLY STATED IN A WRITTEN AGREEMENT BETWEEN THE PARTIES,
// THE SOFTWARE IS PROVIDED AS-IS, WITHOUT WARRANTIES OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
// NONINFRINGEMENT, PERFORMANCE, AND QUALITY.
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

import com.vignette.community.client.services.common.NativeObject;
import com.vignette.community.client.services.common.RemoteObjectEntity;
import com.vignette.community.client.services.common.XAPIConstants;
import com.vignette.community.client.services.dataservice.Action;
import com.vignette.community.client.services.dataservice.ActionData;
import com.vignette.community.client.services.dataservice.CommunityDataServiceFactory;
import com.vignette.community.client.services.dataservice.CommunityDataServiceIf;
import com.vignette.community.client.services.dataservice.CreateNewAction;
import com.vignette.community.client.services.dataservice.GenericAction;
import com.vignette.community.client.services.dataservice.NativeObjectQuery;
import com.vignette.community.client.services.dataservice.Query;
import com.vignette.community.client.services.dataservice.RemoteObjectActionData;
import com.vignette.community.client.services.dataservice.RemoteObjectQuery;
import com.vignette.community.client.services.dataservice.RemoteObjectQueryCreate;
import com.vignette.community.client.services.dataservice.Response;
import com.vignette.community.client.services.dataservice.SimpleActionData;
import com.vignette.community.client.services.util.Util;

import javax.servlet.http.HttpServletResponse;

/**
 * Community Data example - Working with Tags. </br></br> For this example to
 * work the <code>vgncommsvcs.properties</code> should be found in the classpath
 * and should contain an entry based on the following: </br> <code>
 * CommunityServer1=collab,1,&lt;community_server_host&gt;,&lt;community_server_port&gt;,/xapi/,/xapi/,com.vignette.community.client.services.security.VssSecurityHandler,vssKey=changeme
 * </code>
 */
public class CDataTags {

    public String createRemoteObject(CommunityDataServiceIf dservice) {
        // Create a Remote Object
        String extobjid = "12345";
        String extobjcontext = "Sports Channel";
        String extsystemid = "1";
        String extsystemtype = "CMS";
        String extrealm = "News Site";
        String extobjtype = "Article";
        String otherval = XAPIConstants.EXT_OBJ_STATUS
                + "="
                + XAPIConstants.DEV_STATUS;

        RemoteObjectEntity ro = new RemoteObjectEntity(extobjid,
                extobjcontext, extsystemtype,
                extsystemid, extrealm, extobjtype,
                otherval);

        String ROName = "What is the most popular sport?";
        String RODescription = "Story comparing popular sports";

        Action createAction = new CreateNewAction();
        ActionData remoteActionData =
                new RemoteObjectActionData(
                        ROName,
                        RODescription,
                        ro);
        RemoteObjectQueryCreate createQuery =
                new RemoteObjectQueryCreate(ro,
                        createAction,
                        remoteActionData,
                        false);
        Response response1 = dservice.query(createQuery);
        if (response1 != null
                && response1.getResponseCode()
                == HttpServletResponse.SC_MOVED_PERMANENTLY) {
            return Util.getRedirectOid(response1);
        }
        return null;
    }

    private void addTagsToRemoteObject(CommunityDataServiceIf dservice,
                                       String oid) {
        Action manageTagsAction = new GenericAction("ManageTags");
        SimpleActionData simpleActionData = new SimpleActionData();
        simpleActionData.addParams(
                "operation",
                "addTags");
        simpleActionData.addParams(
                XAPIConstants.ObjectFieldName,
                oid);
        simpleActionData.addParams(
                "tags",
                "list of space separated tags");
        Response response1
                = dservice.execute(manageTagsAction, simpleActionData);
        if (response1 != null
                && response1.getResponseCode()
                == HttpServletResponse.SC_MOVED_PERMANENTLY) {
        }
    }

    private void getTagsOnRemoteObject(CommunityDataServiceIf dservice,
                                       String oid) {
        NativeObject obj = new NativeObject(oid, "remoteobject", null);
        NativeObjectQuery query = new NativeObjectQuery(obj);
        query.addAdditionalParam(
                XAPIConstants.INCLUDES,
                XAPIConstants.TAGS);
        Response response1 = dservice.query(query);
        if (response1 != null
                && response1.getResponseCode()
                == HttpServletResponse.SC_OK) {
            String responseXML = response1.getData();
            // Parse out the tags from this XML response.
        }
    }

    private void getTagCloud(CommunityDataServiceIf dservice) {
        String extobjid = null;
        String extobjcontext = null;
        String extsystemid = "1";
        String extsystemtype = "CMS";
        String extrealm = "News Site";
        String extobjtype = "Article";

        // This remoteObjectEntity will find all Remote Objects
        // from the realm "News Site" of type "Article".
        RemoteObjectEntity roEntity = new RemoteObjectEntity(extobjid,
                extobjcontext,
                extsystemtype,
                extsystemid,
                extrealm,
                extobjtype);

        Query query = new RemoteObjectQuery(roEntity);
        query.addAdditionalParam(
                XAPIConstants.UBER_TAGCLOUD,
                "true");
        query.addAdditionalParam(
                XAPIConstants.UBER_TAGCLOUD_LIMIT,
                "30");
        Response response1 = dservice.query(query);
        if (response1 != null
                && response1.getResponseCode()
                == HttpServletResponse.SC_OK) {
            String responseXML = response1.getData();
            // Parse out the tags from this XML response.
        }

    }

    public static void main(String[] args) {

        String communityServerId = "CommunityServer1";
        String currentUser = "admin";
        String password = null;

        CommunityDataServiceIf dservice =
                CommunityDataServiceFactory.getInstance()
                        .createCommunityDataService(
                                communityServerId,
                                currentUser,
                                password);

        CDataTags cdata = new CDataTags();

        String oid = cdata.createRemoteObject(dservice);
        cdata.addTagsToRemoteObject(dservice, oid);
        cdata.getTagsOnRemoteObject(dservice, oid);
        cdata.getTagCloud(dservice);

    }


}
